#include "stack.h"
#include <stdlib.h>

stack_t* InitStack(datatype data)
{
	stack_t* stack = (stack_t*)malloc(sizeof(stack_t));
	if (stack == NULL)
		return NULL;
	stack->data = data;
	stack->next = NULL;
	return stack;
}

int PushStack(stack_t** stack, datatype data)
{
	if (stack == NULL)
		return STACKISNULL;
	stack_t* new_node = InitStack(data);
	if (new_node == NULL)
		return MEMORYMALLOCFALSE;
	new_node->next = *stack;
	*stack = new_node;
	return 0;
}

int PopStack(stack_t** stack, datatype* e)
{
	if (stack == NULL)
		return STACKISNULL;
	if (*stack == NULL)
		return STACKISNULL;
	stack_t* temp = *stack;
	*e = temp->data;
	*stack = (*stack)->next;
	free(temp);
	return 0;
}

int Deepth(stack_t* stack)
{
	int len = 0;
	for (; stack != NULL; ++len)
		stack = stack->next;
	return len;
}

void DestroyStack(stack_t** stack)
{
	if (stack == NULL)
		return;
	if (*stack == NULL)
		return;
	stack_t* temp;
	while (*stack != NULL) {
		temp = *stack;
		*stack = (*stack)->next;
		free(temp);
	}
}